package com.dituhui.web.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.dituhui.tool.io.Constants;
import com.dituhui.tool.io.KafkaTool;
import com.dituhui.tool.util.StringUtil;
import com.dituhui.web.util.UserInfoUtil;

public class Gateway extends BaseService {
	private static final Logger LOGGER = Logger.getLogger(Gateway.class);
	static String kafkaUrl;
	static String kafkaTopic = "GPS";

	public void init() throws ServletException {
		kafkaUrl = Constants.getValue("kafkaUrl");
		LOGGER.info("kafkaUrl=" + kafkaUrl);
	}

	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		super.service(request, response);

		String groupId = request.getParameter("groupId");
		boolean isGroupIdValid = UserInfoUtil.isGroupIdValid(groupId);
		StringBuilder output = new StringBuilder();
		if (!isGroupIdValid) {
			output.append("groupId非法");
		} else {
			String accountId = request.getParameter("accountId");
			// 上传坐标
			if (accountId == null) {
				String message = "参数非法，应传入accountId";
				LOGGER.info(message);
				output.append(message);
			} else {
				String gpsData = request.getParameter("gpsData");
				// System.out.println("gpsData = " + gpsData);
				if (gpsData != null) {
					// String message = x + StringUtil.AngularBracket + y + StringUtil.AngularBracket + accuracy + StringUtil.AngularBracket + time +
					// StringUtil.AngularBracket + groupId + StringUtil.AngularBracket + phone;
					List<String> messages = new ArrayList<String>();
					// 上传多个坐标时，以DoubleUnderline作为分隔符；每个坐标的属性以AngularBracket作为分隔符。
					if (gpsData != null && gpsData.contains(StringUtil.DoubleUnderline)) {
						String[] arr = gpsData.split(StringUtil.DoubleUnderline);
						for (String s : arr) {
							messages.add(s);
						}
						LOGGER.info("reuploaded gpsData=" + gpsData);
					} else if (gpsData != null && gpsData.contains(StringUtil.AngularBracket)) {
						messages.add(gpsData);
					}

					if (messages.size() > 0) {
						for (String message : messages) {
							HashMap<String, String> data = new HashMap<String, String>();
							data.put(accountId, message);
							KafkaTool.produce(kafkaUrl, kafkaTopic, data);
							LOGGER.debug("after KafkaTool.produce()");
						}
						output.append("上传" + messages.size() + "条坐标成功");
					} else {
						long t = System.currentTimeMillis();
						LOGGER.info("参数非法.a=" + accountId + ",g=" + gpsData + ",t=" + t);
						output.append("参数非法,t=" + t);
					}
				} else {
					long t = System.currentTimeMillis();
					LOGGER.info("参数非法.a=" + accountId + ",g=" + gpsData + ",t=" + t);
					output.append("参数非法. gpsData=" + gpsData + ",t=" + t);
				}
			}
		}
		PrintWriter out = response.getWriter();
		out.print(output.toString());
		out.flush();
		out.close();
	}
}
